
/**
 * Date:2019年6月4日上午11:51:59
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;

import com.xue.im.client.core.IMClientContext;

import io.netty.util.internal.StringUtil;

@SuppressWarnings("serial")
public class LoginDialog extends JFrame {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfUsername;
	private JPasswordField tfPassword;

	private static final int DIALOG_WIDTH = 414;
	private static final int DIALOG_HEIGHT = 300;
	static LoginDialog instance;
	private String serverIp;
	private int port;
	private JLabel lbMsg;

	public static LoginDialog getInstance() {
		if (instance == null) {
			instance = new LoginDialog();
		}
		return instance;
	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
			if ("Windows".equals(info.getName())) {
				// if ("Nimbus".equals(info.getName())) {
				try {

					// Metal
					// Nimbus
					// CDE/Motif
					// Windows
					// Windows Classic

					// com.laf.WebLookAndFeel.globalControlFont = new FontUIResource("隶书",0, 12);
					// UIManager.setLookAndFeel ( "com.alee.laf.WebLookAndFeel" );
					UIManager.setLookAndFeel(info.getClassName());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		setTitle("登陆IM2019");
		setIconImage(IMClientContext.client.getImage());
		setAlwaysOnTop(true);
		setResizable(false);
		setBounds(400, 100, DIALOG_WIDTH, DIALOG_HEIGHT);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		// 设置居中
		setLocation(WindowXY.getXY(LoginDialog.this.getSize()));
		// 登录错误信息
		lbMsg = new JLabel();
		lbMsg.setForeground(Color.red);
		lbMsg.setBounds(132, 205, 150, 30);
		contentPanel.add(lbMsg);

		JButton btLogin = new JButton("登 录");
		btLogin.setBounds(132, 230, 150, 30);
		contentPanel.add(btLogin);

		tfUsername = new JTextField();
		tfUsername.setBounds(133, 147, 150, 25);
		contentPanel.add(tfUsername);
		tfUsername.setColumns(10);

		tfPassword = new JPasswordField();
		tfPassword.setBounds(133, 182, 150, 25);
		contentPanel.add(tfPassword);
		tfPassword.setColumns(10);

		JLabel lblNewLabel = new JLabel("用户名");
		lblNewLabel.setBounds(53, 151, 54, 15);
		contentPanel.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("密 码");
		lblNewLabel_1.setBounds(53, 190, 54, 15);
		contentPanel.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("内部消息系统");
		lblNewLabel_2.setBounds(0, 0, DIALOG_WIDTH, 136);
		ImageIcon icon = new ImageIcon(LoginDialog.class.getResource("/login.jpg"));
		icon = ImageScale.getImage(icon, lblNewLabel_2.getWidth(), lblNewLabel_2.getHeight());
		lblNewLabel_2.setIcon((icon));
		contentPanel.add(lblNewLabel_2);

		// 登录逻辑
		btLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String username = tfUsername.getText();
				String password = new String(tfPassword.getPassword());
				if (StringUtil.isNullOrEmpty(username) || StringUtil.isNullOrEmpty(password)) {
					JOptionPane.showMessageDialog(LoginDialog.this, "用户和密码不能为空!", "系统提示", JOptionPane.WARNING_MESSAGE);
					return;
				}
				// 注册自己,并启动客户端,发送登录消息
				IMClientContext.get().setUserid(username).setPassword(password).startClient(serverIp, port);
			}
		});
		// 关闭的钩子
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				// 清理上下文对象
				IMClientContext.get().shutdown();
				// 关闭
				System.exit(0);
			}
		});

	}

	// 刷新用户列表
	public void refreshMsg(String msg) {
		SwingUtilities.invokeLater(() -> {
			lbMsg.setText(msg);
		});
	}

	public void startup(String serverIp, int port) {
		this.serverIp = serverIp;
		this.port = port;
		this.setVisible(true);
	}
}
